/*
 * file name   : SequenceUtil.java 
 * <br>copyright   : umpay 2012
 * <br>description : TODO<何时使用>[注意][示例]
 * <br>modified    : 
 * @author      王世院
 * @version     1.0
 * @date        May 3, 2012 3:59:59 PM
 */ 

package com.umpay.privi.util;

import com.umpay.common.persist.ibatis.DataAccessException;
import com.umpay.dao.call.CommonCallDao;
import com.umpay.dao.impl.DaoFactory;
import com.umpay.privi.exception.BusinessException;

/*************************************************************************
 * <br>description : 序列数工具类
 * @author      王世院
 * @date        May 3, 2012 3:59:59 PM
 * @version     1.0  
 *************************************************************************/

public class SeqUtil {
	/**
	 * description: 生成长度为一定长的序列数
	 * @param is
	 * @param width
	 * @return
	 */
	public static String genSequence(String name, int length) throws BusinessException{
		String s;
		try {
			CommonCallDao commonDao = DaoFactory.getCommonCallDao();
			s = String.valueOf(commonDao.getBatchSeq(name));
		} catch (DataAccessException e) {
			throw new BusinessException("00060590", e);
		} 
		return formatSequence(s, length);
	} 
	/**
	 * desc: 生成序列数，用于生成UserId,roleId等
	 * <p>创建人：Ding Zhe , May 5, 2013 2:24:46 PM</p>
	 * @param name
	 * @return
	 * @throws BusinessException
	 */
	public static int genSequence(String name) throws BusinessException{
		try {
			CommonCallDao commonDao = DaoFactory.getCommonCallDao();
			return commonDao.getBatchSeq(name);
		} catch (DataAccessException e) {
			throw new BusinessException("00060590", e);
		} 
	} 
	
	/**
	 * description: 将传入的字符串的长度限制为一定值
	 * @param is
	 * @param width
	 * @return
	 */
	private static String formatSequence(String is, int width) {
		if (is.length() < width)
			for (; is.length() < width; is = "0" + is);
		else
			is = is.substring(is.length() - width, is.length());
		return is;
	}
}
